home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-04-02 | 3.5 KB | 127 lines | [TEXT/CWIE] |
- /*************************************************************************************
- #
- # PICTEncode.cp
- #
- #
- # Author: Timothy Carroll
- # Apple Developer Technical Support
- # timc@apple.com
- #
- # Modification History:
- #
- # 4/2/97 TMC PICTEncode would double-dispose of the PICT. I wasn't NULLing
- # out the PICT local after disposing of it explicitly in the loop. Spotlight found this.
- # 2/24/97 TMC Now explicitly include main.h
- # 8/15/96 TMC Initial Release
- #
- #
- #
- # Copyright © 1996 Apple Computer, Inc., All Rights Reserved
- #
- #
- # You may incorporate this sample code into your applications without
- # restriction, though the sample code has been provided "AS IS" and the
- # responsibility for its operation is 100% yours. However, what you are
- # not permitted to do is to redistribute the source as "DSC Sample Code"
- # after having made changes. If you're going to re-distribute the source,
- # we require that you make it clear in the source that the code was
- # descended from Apple Sample Code, but that you've made changes.
- #
- *************************************************************************************/
-
- #include "Main.h"
- #include "TGraphic.h"
- #include "PictEncode.h"
-
-
- OSStatus PICTEncode (short inputFileResNum, short outputFileResNum)
- {
- OSStatus theErr;
-
- UInt16 numPicts, loop;
-
- // we pass these to GetResInfo so that we can get the actual resource ID.
- SInt16 resID;
- ResType resType;
- Str255 resName;
-
- // Holds the last value on the resource chain since we change the top resource a lot
- SInt16 saveResNum;
-
- // Temporarily holds the resource so we can get information on it
- Handle pict = NULL;
- TGraphic *compiled = NULL;
-
- saveResNum = CurResFile();
-
- UseResFile (inputFileResNum);
-
-
- // First thing is to copy the color table resource to the output.
- theErr = CopyResource (inputFileResNum, outputFileResNum,'clut', kAppColorTableResID);
- FAIL_OSERR (theErr, "\pFailed to copy the color table to the destination file")
-
-
- // get the color table
- gAppColorTable = GetCTable( kAppColorTableResID );
- FAIL_NIL (gAppColorTable, "\pFailed to open the color table")
-
- // determine the number of PICT resources
- numPicts = Count1Resources( 'PICT' );
-
-
- // get each one,
- for( loop = 1; loop <= numPicts; loop++ )
- {
- // load the pict
- UseResFile (inputFileResNum);
-
- // we'll get the pict first so that we can get the information out of it.
- pict = Get1IndResource( 'PICT', loop );
- theErr = ResError();
- FAIL_NIL (pict, "\pFailed to load the picture")
- FAIL_OSERR (theErr, "\pFailed to load the picture")
-
- // determine its id and name
- GetResInfo( pict, &resID, &resType, resName );
- theErr = ResError();
- FAIL_OSERR( theErr, "\pFailed to get info on the resource")
-
- ReleaseResource (pict);
- pict = NULL; // We're done with this PICT, NULL it out so we don't double dispose
-
-
- // Okay, we know the PICT's resID, build the compiled graphic and write it to the output file.
- compiled = TGraphic::NewGraphic (resID);
- FAIL_NIL (compiled, "\pFailed to compile the TGraphic")
-
- UseResFile (outputFileResNum);
-
- compiled->WriteToGraphicResource();
- compiled->WriteToPICTResource();
- compiled->DisposeReference();
-
- compiled = NULL; // Make sure we don't double dispose of the compiled sprite
-
- }
- // restore the previous port and device
-
- goto cleanup;
-
- error:
-
- if (theErr == noErr)
- theErr = paramErr;
- cleanup:
-
- UseResFile (saveResNum);
-
- if (pict)
- ReleaseResource (pict);
- if (compiled)
- compiled->DisposeReference();
- if (gAppColorTable != NULL)
- DisposeCTable (gAppColorTable);
- gAppColorTable = NULL;
- return theErr;
- }